<script>
	import LocalDAO from 'common/localDAO'
	import RemoteDAO from 'common/remoteDAO'

	export default {
    	data () {
      		return {
      			base : {
      				nickname: LocalDAO.getNickname(),
                	ranknum : '-',
                	total_profitrate : '-',
                	income_balance : '-',
                	total_balance : '-',
                	avail_balance : '-',
                    initmoney: '-',
                	position : '-',
                	isNone : true,
                	isRed : false,
                	isGreen : false,
                	userHead : '',
                	market_balance :'-'
      			},
      			week_increaserate:'-',
                max_drawdown: '-',
                week_profit: '-'
      		}
    	},
    	props: {
    	  pageStatus: {
    	  	type: String
    	  }
    	},
    	mounted () {
    	  this.fetchRateDate()
          this.fetchData()
          this.getPersonDetail()
    	},
        computed: {
          holdRate: function () {
          	let nocolor = true, redcolor = false, greencolor = false
          	if (this.base.income_balance !== '-' || this.base.income_balance !== ''){
              nocolor = false
          	  redcolor = Number(this.base.income_balance) >= 0 ? true : false
          	  greencolor = !redcolor
          	  this.base.income_balance = Number(this.base.income_balance) >= 0 ? ('+' + this.changeTwoDecimal(this.base.income_balance)) : this.base.income_balance
          	}
          	return {
          	  'rateline': true,
          	  'green-value': greencolor,
          	  'red-value': redcolor,
          	  'nocolor-value': nocolor
          	}
          }
        },
    	methods: {
    		fetchData(){
        		const userCode = this.pageStatus === 'self' ? LocalDAO.getUserCode() : this.$route.query.userCode;
        		const gameId = this.$route.query.gameId || "";
        		//查询比赛的持仓详情
        		RemoteDAO.callJCL11143(userCode,gameId,data => {
            		if(data.error == "0"){
                		let list = data.data.body.record;
                		let positionData = list;
                		if(positionData.length > 0){
                    		//整理返回的数据格式
                    		const ob = positionData[0];
							Object.assign(ob,
    										{
    											userHead : this.base.userHead,
    											nickname : this.base.nickname,
    											total_profitrate : parseFloat(list[0].total_profitrate).toFixed(2),
    											income_balance : (Number(list[0].income_balance)/Number(list[0].total_balance)*100).toFixed(2),
    											total_balance : Number(list[0].total_balance).toFixed(2),
    											market_balance : Number(list[0].market_balance).toFixed(2),
    											avail_balance : Number(list[0].avail_balance).toFixed(2),
    											initmoney: Number(list[0].initmoney).toFixed(2),
    											position : Number(list[0].position).toFixed(2),
    											ranknum : list[0].ranknum 
    										});
    						this.$data.base = ob;
    						
    						if (this.pageStatus === 'self'){
    						  //存储数据
                    		  this.saveShareData();
    						}
                    		//控制累计收益率的背景颜色
                    		if(Number(this.base.total_profitrate) >= 0){
                    			this.base.total_profitrate = '+' + this.changeTwoDecimal(this.base.total_profitrate)
                    			this.base.isNone = false;
                    			this.base.isRed = true;
                    		}else{
                    			this.base.isNone = false;
                    			this.base.isGreen = true;
                    		}
                		}
            		}else{

            		}
        		});
            },
            fetchRateDate () {
              const userCode = this.pageStatus === 'self' ? LocalDAO.getUserCode() : this.$route.query.userCode
              const gameId = this.$route.query.gameId || ""
              // const gameId = '201705210050005121450'
              // let userCode = '1000001'
              let param = {userCode: userCode, gameId: gameId}
              RemoteDAO.getUserSubteamList(param).then(res => {
                if (res.resultCode === '000000' && res.data.nrngSubteamUser !== '') {
                  this.week_increaserate = Number(JSON.parse(res.data.nrngSubteamUser).weekIncreaserate).toFixed(2) + '%'
                  this.max_drawdown = Number(JSON.parse(res.data.nrngSubteamUser).maxDrawdown).toFixed(2) + '%'
                  this.week_profit = Number(JSON.parse(res.data.nrngSubteamUser).weekProfit).toFixed(2) + '%'
              	}
              	console.log(JSON.stringify(res))
              }, err => {
              	console.log(err)
              })
            },
            /*
             * 存储需要分享出去的数据到localStorage中
             */
            saveShareData(){
            	let data = {
                		"nickname": this.base.nickname,
                		"userHead":this.base.userHead,
                		"total_profitrate": this.base.total_profitrate,
                		"total_balance": this.base.total_balance,
                		"ranknum": this.base.ranknum,
                		"position": this.base.position,
                		"initmoney": this.base.initmoney,
                		"market_balance": this.base.market_balance,
                		"income_balance": this.base.income_balance,
                		"avail_balance": this.base.avail_balance,
                		"week_increaserate": this.week_increaserate,
                        "max_drawdown": this.max_drawdown,
                        "week_profit": this.week_profit
            	};
            	// 更新分享页数据
        		let gameSelfDetail = LocalDAO.getShareGameSelfDetails() || {};
        		gameSelfDetail.gameAsset = data;
        		LocalDAO.setShareGameSelfDetails(gameSelfDetail);
    		},
    		getPersonDetail () {
    			if (this.pageStatus === 'self') {
    				this.base.nickname = LocalDAO.getNickname()
                    this.base.userHead = LocalDAO.getUserHead()
                    if(this.base.userHead  === ""){
                      this.base.userHead =  "https://dl.app.gtja.com/pictures/niutou/tou" + (parseInt(LocalDAO.getUserCode())%8+1)  + "@2x.png"
                    }
    			} else {
    		      let myUsercode = LocalDAO.getUserCode()
            	  RemoteDAO.callUC12008316(myUsercode,this.$route.query.userCode,data => {
                	if(data.error == "0"){
                      let userContent = data.data.body
                      if(userContent.length > 0){
                        if(userContent[0].USER_HEAD === ""){
                          this.base.userHead = "https://dl.app.gtja.com/pictures/niutou/tou" + (parseInt(myUsercode)%8+1)  + "@2x.png"
                         }else{
                           this.base.userHead = userContent[0].USER_HEAD
                         }
                    	 this.base.nickname = userContent[0].NICKNAME
                      }
                	}
            	  })
    			}
            },
            toPersonDetail () {
              if (this.pageStatus === 'self') {
                this.$router.push({path: '/selfdetail'})
              } else {
              	this.$router.push({path: '/otherdetail', query: {userCode: this.$route.query.userCode}})
              }
            },
            changeTwoDecimal (num) {
              let numF = parseFloat(num)
              if (isNaN(numF)) {
              	return '0.00'
              }
              numF = Math.round(num*100)/100
              let numFStr = numF.toString()
              console.log('加小数点', numFStr)
              let hasDecimal = numFStr.indexOf('.')
              if (hasDecimal < 0) {
              	numFStr += '.00'
              }
              return numFStr
            }

       }
  }
</script>